from __future__ import annotations

from dataclasses import dataclass
from typing import Tuple


@dataclass
class SagOptimizationResult:
    best_sag_ratio: float
    min_tension_kN: float
    note: str


def optimize_sag_for_tension(span_m: float, conductor_weight_kN_m: float, sag_bounds: Tuple[float, float] = (0.015, 0.035)) -> SagOptimizationResult:
    """在给定弧垂比范围内寻找最小张力的方案（抛物线近似）。"""

    l = span_m
    w = conductor_weight_kN_m
    best_ratio = sag_bounds[0]
    min_tension = 1e18
    for r in [sag_bounds[0] + i * (sag_bounds[1] - sag_bounds[0]) / 100.0 for i in range(101)]:
        f = r * l
        H = w * l * l / max(1e-6, 8.0 * f)
        if H < min_tension:
            min_tension = H
            best_ratio = r
    return SagOptimizationResult(best_sag_ratio=best_ratio, min_tension_kN=min_tension, note="忽略风冰与温度影响的初步优化")


